//
//  NewsController.swift
//  SCA
//
//  Created by tjun on 2025/9/1.
//

import UIKit
import SnapKit

class NewsController: BaseController {

    private var scrollView: UIScrollView!
    private let overviewButton = SelectableGradientLineButton()
    private let powerListButton = SelectableGradientLineButton()
    private var segmentedControl = UISegmentedControl(items: ["资讯".localizedString, "手册指南".localizedString])
    var selectedIndex: Int = 0 {
        didSet {
            if self.isViewLoaded {
                segmentedControl.selectedSegmentIndex = selectedIndex
                scrollView.contentOffset = CGPointMake(CGFloat(selectedIndex) * self.scrollView.width, 0)
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBarUI()
        
        segmentedControl.selectedSegmentIndex = selectedIndex
        segmentedControl.backgroundColor = Assets.Color.segmentedControlBg
        segmentedControl.selectedSegmentTintColor = Assets.Color.selectedSegmentTintColor
        segmentedControl.addTarget(self, action: #selector(segmentedChanged(_:)), for: .valueChanged)
        segmentedControl.setTitleTextAttributes([
            .foregroundColor: Assets.Color.segmentedControlTitle,
            .font: UIFont.systemFont(ofSize: 14, weight: .medium)
        ], for: .normal)
        
        segmentedControl.setTitleTextAttributes([
            .foregroundColor: Assets.Color.segmentedControlSelectTitle,
            .font: UIFont.systemFont(ofSize: 14, weight: .medium)
        ], for: .selected)
        
        segmentedControl.layer.cornerRadius = 8
        segmentedControl.layer.masksToBounds = true
        view.addSubview(segmentedControl)
        segmentedControl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(navBarHeight + 10)
            make.height.equalTo(36)
            make.width.equalTo(180)
        }
  
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 56 + navBarHeight, width: view.width, height: screenHeight - navBarHeight - 44)).addTo(view)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.addTo(view)
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height)
        
        let newsListController = NewsListController(type: .news)
        addChild(newsListController)
        scrollView.addSubview(newsListController.view)
        newsListController.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height)
        
        let manualGuideListController = NewsListController(type: .guide)
        addChild(manualGuideListController)
        scrollView.addSubview(manualGuideListController.view)
        manualGuideListController.view.frame = CGRectMake(scrollView.width, 0, scrollView.width, scrollView.height)
        
        scrollView.contentOffset = CGPointMake(CGFloat(selectedIndex) * self.scrollView.width, 0)
        
    }
    
    private func setNavigationBarUI() {
        
        navigationBar.title = nil
        
        let infoButton = UIButton(image: Assets.Image.nav_mine_info, target: self, action: #selector(infoButtonTapped))
        navigationBar.contentView.addSubview(infoButton)
        infoButton.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        let logoImageView = UIImageView(image: Assets.Image.nav_logo)
        navigationBar.contentView.addSubview(logoImageView)
        logoImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let logoutButton = UIButton(image: Assets.Image.nav_logout, target: self, action: #selector(logoutButtonTapped))
        navigationBar.contentView.addSubview(logoutButton)
        logoutButton.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
    }
    
    @objc func infoButtonTapped() {
        let mineController = MineController()
        mineController.showInController(self)
    }
    
    @objc func logoutButtonTapped() {
        NotificationCenter.default.post(name: NotificationName.userLogout, object: nil)
    }
 
    @objc private func segmentedChanged(_ sender: UISegmentedControl) {
         print("当前选中索引：\(sender.selectedSegmentIndex)")
         print("当前选中标题：\(sender.titleForSegment(at: sender.selectedSegmentIndex) ?? "")")
        self.scrollView.contentOffset = CGPointMake(CGFloat(sender.selectedSegmentIndex) * self.scrollView.width, 0)
     }
}


extension NewsController: UIScrollViewDelegate {
   func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
       let selectedIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
       printf("selectedIndex=\(selectedIndex)")
       segmentedControl.selectedSegmentIndex = selectedIndex
   }
   
}

